package com.gxa.service.shezhiservice.Impl;

import com.gxa.common.R;
import com.gxa.mapper.shezhimapper.QuanXianMapper;
import com.gxa.pojo.QuanXian;
import com.gxa.pojo.User;
import com.gxa.pojo.UserQuanXian;
import com.gxa.service.shezhiservice.QuanXianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author weishen
 */
@Service
public class QuanXianServiceImpl implements QuanXianService {

    @Autowired
    QuanXianMapper quanXianMapper;

    R r= new R();

    @Override
    public R updateQuanXianById(ArrayList<UserQuanXian> userQuanXians) {
        for(int i=0;i<userQuanXians.size();i++)
        {
            if(userQuanXians.get(i).getStatus()==0)
            {
                quanXianMapper.deleteQuanXianById(userQuanXians.get(i).getUserId(),userQuanXians.get(i).getQuanXianId());
            }
            else
            {
                quanXianMapper.insertQuanXian(userQuanXians.get(i).getUserId(),userQuanXians.get(i).getQuanXianId());
            }
        }
        r.setCode(200);
        r.setMsg("更新成功");
        return r;
    }
    @Override
    public R selectAllQuanXian() {
        List<QuanXian> quanXians = quanXianMapper.selectAllQuanXian();
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setData(quanXians);
        return r;
    }

    @Override
    public R selectAllUserAndQuanXianForPage(int page,int size) {
        int index = (page-1)*size;
        List<User> list = quanXianMapper.selectAllUserAndQuanXianForPage(index, size);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setData(list);
        return r;
    }

    @Override
    public R insertUser(User user) {
        int res = quanXianMapper.insertUser(user);
        r.setCode(500);
        r.setMsg("插入失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("插入成功");
        }
        return r;
    }

    @Override
    public R updateQuanXianLogin(int user_id, int quanXianLogin) {
        int res = quanXianMapper.updateQuanXianLogin(user_id,quanXianLogin);
        r.setCode(500);
        r.setMsg("更新失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("更新成功");
        }
        return r;
    }

    @Override
    public R updatePasswordById(int user_id, String password) {
        int res = quanXianMapper.updatePasswordById(user_id,password);
        r.setCode(500);
        r.setMsg("更新失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("更新成功");
        }
        return r;
    }
}
